package com.cy.store.controller;

import com.cy.store.controller.ex.*;
import com.cy.store.service.ex.*;
import com.cy.store.util.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

/**
 * @PROJECT_NAME: store
 * @DESCRIPTION:
 * @USER: 28416
 * @DATE: 2022/11/2 20:34
 *
 * 用来表示控制层类的基类
 *
 */
public class BaseController {


    public  static  final  int OK = 200;  //表示操作成功的状态码

    //请求处理方法，这个方法的返回值就是需要传递给前端的数据

    //当项目中产生异常时，会被拦截到此方法  这个方法此时充当的就是请求处理方法 方法的返回值直接给到前端
    @ExceptionHandler({ServiceException.class,FileLoadException.class}) // 主要用于统一处理抛出的异常
    public JsonResult<Void> handleException(Throwable e){
        JsonResult<Void>   result = new JsonResult<>(e);
        if (e instanceof UsernameDuplicatedException){
            result.setState(4000);
            result.setMessage("用户名已经被占用的异常");
        }else if (e instanceof AddressCountLimitException){
            result.setState(4001);
            result.setMessage("购物车地址超出限制的异常");
        }else if (e instanceof UsernameNotFoundException){
            result.setState(5001);
            result.setMessage("用户数据不存在的异常");
        }else if (e instanceof PasswordNotMatchException){
            result.setState(5002);
            result.setMessage("用户名的密码错误的异常");
        }else if (e instanceof UpdateException){
            result.setState(5003);
            result.setMessage("更新密码时发生未知的异常");
        }else if (e instanceof InsertException){
            result.setState(5000);
            result.setMessage("注册时发生未知的异常");
        }else if (e instanceof FileEmptyException) {
            result.setState(6000);
            result.setMessage("上传文件为空的异常");
        }else if (e instanceof FileSizeException) {
            result.setState(6001);
            result.setMessage("文件大小超出限制的异常");
        }else if (e instanceof FileStateException) {
            result.setState(6002);
            result.setMessage("文件状态不符合的异常");
        }else if (e instanceof FileUploadIoException) {
            result.setState(6003);
            result.setMessage("文件读写错误的异常");
        }else if (e instanceof FileTypeException) {
            result.setState(6004);
            result.setMessage("文件类型异常的异常");
        }else if (e instanceof AccessDeniedException) {
            result.setState(4005);
            result.setMessage("收货地址数据访问非法的异常");
        }else if (e instanceof AddressNotFoundException) {
            result.setState(4004);
            result.setMessage("用户收货地址不存在的异常");
        }else if (e instanceof DeleteException) {
            result.setState(5004);
            result.setMessage("删除时产生了的异常");
        }else if (e instanceof ProductNotFoundException) {
            result.setState(400);
            result.setMessage("删除时产生了的异常");
        }else if (e instanceof CartNotFoundException) {
            result.setState(4007);
        }else if (e instanceof UserNotInitException) {
            result.setMessage("用户未激活，请激活用户！！！");
            result.setState(4008);
        }
        return  result;
    }

    /**
     * 获取session对象中的uid
     * @param session
     * @return  返回当前当前用户的uid 值
     */
    protected  final Integer getUidFromSession(HttpSession session) {
       return Integer.valueOf(session.getAttribute("uid").toString());

    }
    /**
     * 获取session对象中的username
     * @param session
     * @return 返回当前用户饿用户名
     */
    public final  String getUsernameFromSession(HttpSession session){
        return  session.getAttribute("username").toString();
    }

}
